package com.company.woye.checkscan;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.checkscan.bean.StartScan;
import com.company.woye.internet.KjjPHCallBack;
import com.company.woye.taiorcheckSize.TaiorCheckDetailActivity;
import com.company.woye.taiorcheckSize.TaiorCheckSizeActivity;
import com.company.woye.taiorcheckSize.bean.TaiorSizeBean;
import com.company.woye.utils.Constans;
import com.company.woye.utils.ScannerManager;
import com.company.woye.utils.Toaster;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

public class StartScanActivity extends BaseActivity {

    @BindView(R.id.iv_on_package_back)
    ImageView ivOnPackageBack;
    @BindView(R.id.tv_on_package_back)
    TextView tvOnPackageBack;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.ed_code)
    EditText editBarcode;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_hyh)
    TextView tvHyh;
    @BindView(R.id.tv_dbh)
    TextView tvDbh;
    @BindView(R.id.tv_hh)
    TextView tvHh;
    @BindView(R.id.tv_po)
    TextView tvPo;
    @BindView(R.id.tv_cplx)
    TextView tvCplx;
    @BindView(R.id.tv_pssh)
    TextView tvPssh;
    @BindView(R.id.tv_sc)
    TextView tvSc;
    @BindView(R.id.tv_sh)
    TextView tvSh;
    @BindView(R.id.tv_js)
    TextView tvJs;
    @BindView(R.id.tv_cm)
    TextView tvCm;
    @BindView(R.id.tv_all_bs)
    TextView tvAllBs;
    @BindView(R.id.tv_ys_bs)
    TextView tvYsBs;
    @BindView(R.id.tv_all_js)
    TextView tvAllJs;
    @BindView(R.id.tv_ys_js)
    TextView tvYsJs;
    @BindView(R.id.rl)
    RelativeLayout rl;
    private ScannerManager scannerManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (scannerManager.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_start_scan;
    }

    String cjName = "", bigDept = "", bmName = "", lsx = "", inputMan = "";
    String inputId = "", kdrq = "", token = "", txm = "", ygh = "", yghXm = "";

    @Override
    public void initView() {
        editBarcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.showSoftInput(editBarcode, InputMethodManager.SHOW_FORCED);
            }
        });
        editBarcode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event != null) {
                    if ((event.getAction() == KeyEvent.ACTION_UP) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        return true;
                    }
                }
                return false;
            }
        });
        editBarcode.requestFocus();
        editBarcode.requestFocusFromTouch();
        //拦截扫描事件
        scannerManager = new ScannerManager(new ScannerManager.OnScanListener() {
            @Override
            public void onResult(String code, String deviceName) {
                String nowCode = dealCode(code).trim();
                if (nowCode.length() > 12 || nowCode.length() == 12) {
                    nowCode = nowCode.substring(nowCode.length() - 12);
                    editBarcode.setText(nowCode);
                    scanCode();
                }
            }
        }, editBarcode.getContext());

        cjName = getIntent().getStringExtra("cjName");
        bigDept = getIntent().getStringExtra("bigDept");
        bmName = getIntent().getStringExtra("bmName");
        lsx = getIntent().getStringExtra("lsx");
        inputMan = getIntent().getStringExtra("inputMan");
        inputId = getIntent().getStringExtra("inputId");
        kdrq = getIntent().getStringExtra("kdrq");
        token = getIntent().getStringExtra("token");
        txm = getIntent().getStringExtra("txm");
        editBarcode.setText(txm);
        ygh = getIntent().getStringExtra("ygh");
        yghXm = getIntent().getStringExtra("yghXm");
        tvPerson.setText(ygh + (yghXm));
        scanCode();
    }

    private void scanCode() {
        /**
         * {
         *   "bbm": "制衣二部",
         *   "bmName": "制衣二部(1)",
         *   "cjName": "裁剪一工场",
         *   "inputId": "C2101105A",
         *   "inputMan": "裁一检验5",
         *   "kdrq": "2021-12-02 00:00:00",
         *   "lsx": "1-5",
         *   "token": "7e0daaaed1a1543727122788a4e5398e7c23e88b075a32e3922a6d96b5642db9",
         *   "txm": "267000980263",
         *   "ygh": "何树珍",
         *   "yghXm": "21027477"
         * }
         */

        JSONObject params = new JSONObject();
        try {
            params.put("cjName", cjName);
            params.put("bbm", bigDept);
            params.put("bmName", bmName);
            params.put("lsx", lsx);
            params.put("inputId", inputId);
            params.put("inputMan", inputMan);
            params.put("kdrq", kdrq);
            params.put("txm", editBarcode.getText().toString());
            params.put("ygh", ygh);
            params.put("yghXm", yghXm);
            params.put("token", token);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("提交数据", params.toString());
        Log.e("访问地址", Constans.IP + "szapi/third/BdMlcj/bdJysm");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(params.toString()).url(Constans.IP + "szapi/third/BdMlcj/bdJysm").build().execute(new KjjPHCallBack<StartScan>(StartScan.class, StartScanActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(StartScan response, int id) {
                Toaster.showLong(StartScanActivity.this,response.getMsg());
                if (response.getMsg().equals("102")) {
                    Toaster.showLong(StartScanActivity.this,"扫描扎号成功 ");
                    StartScan.DataBean data = response.getData();
                    tvHyh.setText(data.getHyh());
                    tvDbh.setText(data.getDbh());
                    tvHh.setText(data.getHh());
                    tvPo.setText(data.getPoh());
                    tvCplx.setText(data.getCplx());
                    tvPssh.setText(data.getPsxx());
                    tvSc.setText(data.getSc());
                    tvSh.setText(data.getSh());
                    tvJs.setText(data.getShl());
                    tvCm.setText(data.getCm());
                    tvAllBs.setText(data.getZcount());
                    tvYsBs.setText(data.getYszcount());
                    tvAllJs.setText(data.getZshl());
                    tvYsJs.setText(data.getYszshl());
                }else {
                    tvHyh.setText("");
                    tvDbh.setText("");
                    tvHh.setText("");
                    tvPo.setText("");
                    tvCplx.setText("");
                    tvPssh.setText("");
                    tvSc.setText("");
                    tvSh.setText("");
                    tvJs.setText("");
                    tvCm.setText("");
                    tvAllBs.setText("");
                    tvYsBs.setText("");
                    tvAllJs.setText("");
                    tvYsJs.setText("");
                }
            }
        });
    }

    //保留英文和数字
    private String dealCode(String code) {
        return code.replaceAll("[^a-zA-Z0-9$%]", "");
    }

    @OnClick({R.id.iv_on_package_back, R.id.tv_on_package_back, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_on_package_back:
            case R.id.tv_on_package_back:
                finish();
                break;
            case R.id.tv_sure:
                scanCode();
                break;
        }
    }
}
